package com.example.rbacmanage.service.app.impl;

import com.alibaba.druid.util.StringUtils;
import com.example.rbacmanage.annotation.PermissionRange;
import com.example.rbacmanage.common.Constant;
import com.example.rbacmanage.entity.app.AppUser;
import com.example.rbacmanage.mapper.app.AppUserMapper;
import com.example.rbacmanage.service.app.AppUserService;
import com.example.rbacmanage.util.JwtTokenUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class AppUserServiceImpl implements AppUserService {
    @Autowired
    private AppUserMapper appUserMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    @Override
    public void save(AppUser o) {
        if(!StringUtils.isEmpty(o.getPassword())){
            String passworddecode = passwordEncoder.encode(o.getPassword());
            o.setPassword(passworddecode);
        }
        appUserMapper.insert(o);
    }

    @Override
    public void remove(Long[] ids) {
        for(Long id:ids){
            appUserMapper.del(id);
        }
    }

    @Override
    public void update(AppUser o) {
        if(!StringUtils.isEmpty(o.getPassword())){
            String passworddecode = passwordEncoder.encode(o.getPassword());
            o.setPassword(passworddecode);
        }
        appUserMapper.update(o);
    }

    @Override
    public List<AppUser> getAll(AppUser appUser) {
        return appUserMapper.list(appUser);
    }

    @Override
    @PermissionRange(deptAlias = "d",userAlias = "u")
    public PageInfo<AppUser> getPage(AppUser o) {
        PageHelper.startPage(o.getPage(), o.getLimit());
        return new PageInfo<>(appUserMapper.list(o));
    }

    @Override
    public AppUser getById(Long id) {
        AppUser appUser = appUserMapper.queryOneById(id);
        if (appUser == null){
            return appUser;
        }
        return appUser;
    }

    @Override
    public Integer getCount(AppUser appUser) {
        return 0;
    }

    @Override
    public String login(AppUser appUser){
        String token = JwtTokenUtil.generateToken(appUser.getId()+"");
        Date expireDate=JwtTokenUtil.getExpiredDateFromToken(token);
        redisTemplate.opsForValue().set(Constant.appLoginUserToken+ appUser.getId(),token,expireDate.getTime(), TimeUnit.MILLISECONDS);
        return token;
    }

    @Override
    public String register(AppUser appUserReq) {
        String passworddecode = passwordEncoder.encode(appUserReq.getPassword());
        AppUser appUser = new AppUser();
        appUser.setUserName(appUserReq.getUserName());
        appUser.setPassword(passworddecode);
        appUser.setEmail(appUserReq.getEmail());
        appUser.setPhone(appUserReq.getPhone());
        appUser.setCreateTime(new Date());
        appUser.setUpdateTime(new Date());
        appUser.setBriefIntroduction(appUserReq.getBriefIntroduction());
        appUser.setEmailValidCode(appUserReq.getEmailValidCode());
        appUserMapper.insert(appUser);
        return "注册成功";
    }

    @Override
    public Boolean logout(Long userId) {
        String token = (String) redisTemplate.opsForValue().get(Constant.appLoginUserToken+userId);
        //刷新token
        String tokenNew = JwtTokenUtil.refreshToken(token);
        Date expireDate=JwtTokenUtil.getExpiredDateFromToken(tokenNew);
        redisTemplate.opsForValue().set(Constant.appLoginUserToken+userId,tokenNew,expireDate.getTime(), TimeUnit.MILLISECONDS);
        return true;
    }

    @Override
    public AppUser getByPhone(String phone) {
        return appUserMapper.queryOneByUsername(phone);
    }
}
